package org.jiamandan.com.strategy.impl.car.listener;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jiamandan.com.constant.YxConstant;
import org.jiamandan.com.dto.YXBasicDto;
import org.jiamandan.com.strategy.impl.car.IYXContextListener;
import org.jiamandan.com.utils.CommonUtil;
import org.jiamandan.com.utils.YxObjectUtil;
import org.springframework.stereotype.Service;

/** 充电桩主动申请启动充电【账号密码/刷卡】
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @author:Lee
 * @email:honvy.lee@qq.com
 * @see:
 * @创建日期：2023-03-30 14:38
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2023-03-30 14:38	         Lee        	创建
 * @end
 */
@Service(value = YxConstant.Service.CMD_31)
@AllArgsConstructor
@Slf4j
public class YxApplyRechargeListener implements IYXContextListener {

    private static final int[] number = {14, 2, 2, 2, 16, 32, 34};

    @Override
    public void doExecute(YXBasicDto dto, Channel client) throws Exception {
        int cursor = 0, length = dto.getData().length();
        log.info("YX读取到信息={}",dto.getData());
        int start = 0, end = number[cursor];
        String pileNo = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String gunNo = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String startMethod = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String isNeedPwd = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String account = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String password = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String vin = StringUtils.substring(dto.getData(), start, end);
        //获取到充电枪信息
       // String gunNo = StringUtils.substring(dto.getData(), 14, 2);
        StringBuilder builder = new StringBuilder();
        builder.append("0004"); //系列号域
        builder.append("00");
        builder.append(StringUtils.leftPad("869069749701578752",32,"0"));
        builder.append(StringUtils.leftPad(pileNo, 14, "0"));
        builder.append(gunNo);
        builder.append(account);
        builder.append("00000000");
        builder.append("00");
        //添加流水
        String replyMsg = YxObjectUtil.pushMessage(builder.toString(), YxConstant.Cmd.CMD_32);
        log.info("运营平台确认启动JMD-YX充电(CMD = 32)：" + replyMsg);
        ByteBuf buf = Unpooled.buffer(replyMsg.getBytes().length);
        buf.writeBytes(CommonUtil.hexToByteArray(replyMsg));
        ChannelFuture future = client.writeAndFlush(buf);
        if (future.isDone()) {
        }
    }

/*
    private OrderCharge createOrder(RechargeGun gun, Member member, YXBasicDto dto, String cardNo, Channel client){
        List<OrderCharge> orderList = iOrderChargeService.list(Wrappers.<OrderCharge>query().lambda().eq(OrderCharge::getOrderStatus, BizConstant.OrderStatus.PAID).eq(OrderCharge::getMemberId,member.getId()).gt(BaseEntity::getCreateTime, new Date(DateUtil.now().getTime() - 30000)));
        if(CollectionUtil.isNotEmpty(orderList)){
            log.error(member.getPhoneNo()+":30秒有订单");
            return null;
        }
        long id = IdentityIdUtil.getFlowIdInstance().nextId();
        OrderCharge orderCharge = new OrderCharge();
        orderCharge.setOrderNo(Func.toStr(id));
        orderCharge.setMemberId(member.getId());
        orderCharge.setMemberName(member.getNickName());
        orderCharge.setPhone(member.getPhoneNo());
        orderCharge.setOrgCode(gun.getOrgCode());
        orderCharge.setTenantId(gun.getTenantId());
        orderCharge.setRealPower(BigDecimal.ZERO);
        orderCharge.setChargeAmt(BigDecimal.ZERO);
        orderCharge.setTotalServiceCost(BigDecimal.ZERO);
        orderCharge.setSumChargeTime("00:00:00");
        orderCharge.setDiscountAmt(BigDecimal.ZERO);
        //刷卡还是用余额 10元充电
        if((member.getBalance().add(member.getPresentAmt())).compareTo(BigDecimal.TEN)>=0){
            orderCharge.setPayAmt(BigDecimal.TEN);
            if(member.getBalance().compareTo(BigDecimal.TEN)>=0){
                orderCharge.setBalancePay(BigDecimal.TEN);
                orderCharge.setPresentAmtPay(BigDecimal.ZERO);
            }else{
                orderCharge.setBalancePay(member.getBalance());
                orderCharge.setPresentAmtPay(BigDecimal.TEN.subtract(member.getBalance()));
            }
        }else{
            orderCharge.setPayAmt(member.getBalance().add(member.getPresentAmt()));
            orderCharge.setBalancePay(member.getBalance());
            orderCharge.setPresentAmtPay(member.getPresentAmt());
        }
        orderCharge.setSeizeSeatFee(BigDecimal.ZERO);
        orderCharge.setRefundAmt(BigDecimal.ZERO);
        orderCharge.setOrderAmt(BigDecimal.ZERO);
        orderCharge.setMaxChargePower(BigDecimal.ZERO);
        orderCharge.setChargeDuration(0);
        orderCharge.setProfit(BizConstant.ProfitStatus.PROFIT_NO);
        orderCharge.setSource(BizConstant.Source.CARD);
        orderCharge.setSiteId(gun.getSiteId());

        orderCharge.setChargePileId(gun.getPileId());
        orderCharge.setChargePileNo(gun.getPileNo());
        orderCharge.setChargeGunId(gun.getId());
        orderCharge.setChargeGunNo(gun.getGunNo());
        orderCharge.setCardNo(Func.toStr(cardNo));
        orderCharge.setTotalElectric(BigDecimal.ZERO);
        Site site = iSiteService.getById(gun.getSiteId());
        if (site.getAgentId() != null) {
            orderCharge.setAgentId(site.getAgentId());
        }
        if (site.getMerchantId() != null) {
            orderCharge.setMerchantId(site.getMerchantId());
        }
        if (site.getSalesId() != null) {
            orderCharge.setSalesId(site.getSalesId());
        }
        orderCharge.setAgentCommission(BigDecimal.ZERO);
        orderCharge.setMerchantCommission(BigDecimal.ZERO);
        orderCharge.setSalesCommission(BigDecimal.ZERO);
        orderCharge.setOrderStatus(BizConstant.OrderStatus.UNPAID);
        iOrderChargeService.saveOrUpdate(orderCharge);
        return orderCharge;
    }

*/
}
